package com.shop.shop.controller;


import com.shop.shop.entity.UrCm;
import com.shop.shop.entity.UrCn;
import com.shop.shop.service.CouponService;
import com.shop.shop.utile.JWTUtils;
import com.shop.shop.utile.MapUtile;
import com.shop.shop.utile.Q;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 优惠价  前端控制器
 * </p>
 *
 * @author 小🌤
 * @since 2021-04-22
 */
@RestController
@RequestMapping("/shop/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;


    /**
     * 分页查询和优惠卷
     * @param mapUtile
     * @return
     */
    @GetMapping("list")
    public Q list(MapUtile mapUtile, HttpServletRequest request){
        Integer userId = JWTUtils.getUserId(request.getHeader("token"));
        Map<String,Object> map= couponService.listPage(mapUtile,userId);
        return Q.ok().data(map);
    }

    /**
     * 领取优惠卷
     * @param urCn
     * @param request
     * @return
     */
    @PostMapping("save")
    public Q save(@RequestBody UrCn urCn,HttpServletRequest request){
        Integer userId = JWTUtils.getUserId(request.getHeader("token"));
        urCn.setUrId(userId);
        boolean  cnur= couponService.saveUrCn(urCn);
        return cnur?Q.ok():Q.error();
    }

}

